/* 
 Document   : services
 Created on : Jun 17, 2013, 3:00:54 PM
 Author     : Van
 Description:
 Support show/hide service function, highlight tabs that child item selected
 */

$(document).ready(function() {
        /* Toggle VIP feature */
        $("#vip").click(function(event) {
            if ($(this).is(':checked')) {
                $("#chitiet").css("display", "block"); // VIP ON
            } else {
                $("#chitiet").css("display", "none"); // VIP OFF
            }
        });

        $("#tabs").tabs({
            beforeLoad: function(event, ui) {
                ui.jqXHR.error(function() {
                    ui.panel.html("Loi ket noi");
                });
            },
            load: function(event, ui) {
                $("#tabs-1").css("display", "none");
                //$("#tabs")

            }
        });

        /* Select services in tabs */
        $("#tabs div.item").click(function(event) {
            $parent = $(this).parent();
            $idParent = $parent.attr('id');
            $itemSelected = $parent.children('div.selected').length;
            //console.log('id selected: ' + $idParent);
            $parentTabs = $(this).parent().parent();
            if ($(this).hasClass('selected')) { // Unselected
                $(this).removeClass("selected");
                $(this).find('input').removeAttr('name');
                // restore tabs with item selected
                $parentTabs.find('ul li').each(function(index) {
                    $hrefID = $(this).find('a').attr('href');
                    //console.log($hrefID);
                    if ($hrefID.indexOf($idParent) > 0) {
                        if ($(this).hasClass('ui-state-default-selected')) {
//console.log('added');
                            if (1 === $itemSelected) {
                                $(this).removeClass('ui-state-default-selected');
                            }
                        }
                    }
                });
            } else { // Selected
                $(this).addClass("selected");
                $(this).find('input').attr('name', 'services[]');
                // highlight tabs with item selected
                $parentTabs.find('ul li').each(function(index) {
                    $hrefID = $(this).find('a').attr('href');
                    //console.log($hrefID);
                    if ($hrefID.indexOf($idParent) > 0) {
                        if (!$(this).hasClass('ui-state-default-selected')) {
//console.log('added');
                            $(this).addClass('ui-state-default-selected');
                        }
                    }
                });
            }
        }); // END $("#tabs div.item").click

        /* Submit form add service */
        $("#button_submit").click(function(e) {
            $isVip = $("#vip").is(':checked');
            $serviceSelected = $("#tabs div.selected");
            if ($isVip && (0 === $serviceSelected.length)) {
                e.preventDefault();
                console.log('error');
                alert('Bạn chưa chọn dịch vụ cho VIP.');
            } else if ($isVip && 0 < $serviceSelected.length) {
                console.log('submit');
                document.forms[0].submit(function() {
                    // Submit form
                });
            }
        });

        $("#Cat_ID").live("change", function() {
            $idCat = $(this).attr('value');
            console.log($idCat);
            if ($idCat.lastIndexOf("1") > 0) {
                $("#vip").attr('checked', true);
                $("#chitiet").css("display", "block"); // VIP ON
            } else {
                $("#vip").attr('checked', false);
                $("#chitiet").css("display", "none"); // VIP OFF
            }
        });
});
